
package com.shai.wool.dishonesty.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.shai.wool.common.controller.BaseController;
import com.shai.wool.common.param.SessionUserInfo;
import com.shai.wool.dishonesty.entity.DishonestyVO;
import com.shai.wool.dishonesty.param.DishonestyParam;
import com.shai.wool.dishonesty.service.DishonestyService;
import com.shai.wool.sensitiveword.SensitiveWordUtils;

/**
 * 平台失信信息Controller
 *
 * @author 朱国军
 * @since 1.0
 * @version 2015年12月27日 朱国军
 */
@Controller
public class DishonestyController extends BaseController {
    
    private Logger logger = LoggerFactory.getLogger(DishonestyController.class);
    
    @Autowired
    private DishonestyService dishonestyService;
    
    private final static String LIST_PAGE = "/dishonesty/dishonesty_list_ajax";
    
    private final static String VIEW_PAGE = "/dishonesty/dishonesty_view";
    
    private final static String UPDATE_PAGE = "/dishonesty/dishonesty_edit";
    
    private static final String MY_LIST_PAGE = "/account/my_dishonesty";

    @RequestMapping(value = "/check/dishonesty/toInsert")
    public ModelAndView intoInsert() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        DishonestyVO dishonestyVO = new DishonestyVO();
        resultMap.put("dishonesty", dishonestyVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/check/dishonesty/toUpdate/{id}")
    public ModelAndView intoUpdate(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        DishonestyVO dishonestyVO = dishonestyService.findById(id);
        resultMap.put("dishonesty", dishonestyVO);
        return new ModelAndView(UPDATE_PAGE, resultMap);
    }

    @RequestMapping(value = "/check/dishonesty/update", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String update(DishonestyVO dishonestyVO) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if (SensitiveWordUtils.isContaintSensitiveWord(dishonestyVO.getDishonestyDetail(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "详情描述中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(dishonestyVO.getDishonestyDetail(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }

        if (SensitiveWordUtils.isContaintSensitiveWord(dishonestyVO.getDishonestyTitle(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "失信事件中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(dishonestyVO.getDishonestyTitle(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }
        
        if (SensitiveWordUtils.isContaintSensitiveWord(dishonestyVO.getPlatformName(), 1)) {
            resultMap.put("result", false);
            resultMap.put("message", "平台名称中包含敏感词汇" + SensitiveWordUtils.getSensitiveWord(dishonestyVO.getPlatformName(), 1) + "，请检查后再保存");
            return JSONObject.toJSONString(resultMap);
        }
        
        try {
            Integer affectCount = 0;
            if (StringUtils.isNotBlank(dishonestyVO.getDishonestyId())) {
                affectCount = dishonestyService.update(dishonestyVO);
            } else {
                dishonestyVO.setDishonestyId(UUID.randomUUID().toString());
                dishonestyVO.setPublishUser(seUser.getUserId());
                dishonestyVO.setDataStatus(0);
                affectCount = dishonestyService.insert(dishonestyVO);
            }
            if (affectCount != null && affectCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "新增或修改平台失信信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "新增或修改平台失信信息失败");
            }
        } catch (Exception e) {
            logger.error("新增或修改平台失信信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "新增或修改平台失信信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }

    @RequestMapping(value = "/admin/dishonesty/delete/{id}", produces = "application/json;charset=utf-8;")
    @ResponseBody
    public String delete(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Integer deleteCount = dishonestyService.delete(id);
            if (deleteCount != null && deleteCount == 1) {
                resultMap.put("result", true);
                resultMap.put("message", "删除平台失信信息成功");
            } else {
                resultMap.put("result", false);
                resultMap.put("message", "删除平台失信信息失败");
            }
        } catch (Exception e) {
            logger.error("删除平台失信信息出错", e);
            resultMap.put("result", false);
            resultMap.put("message", "删除平台失信信息出错：" + e.getLocalizedMessage());
        }
        return JSONObject.toJSONString(resultMap);
    }
    
    @RequestMapping(value = "/uncheck/dishonesty/list")
    public ModelAndView queryList(DishonestyParam dishonestyParam) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> dataList = dishonestyService.queryByPage(dishonestyParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", dishonestyParam);
        return new ModelAndView(LIST_PAGE, resultMap);
    }
    
    @RequestMapping(value = "/account/dishonesty/list")
    public ModelAndView queryMyList(DishonestyParam dishonestyParam) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        dishonestyParam.setPublishUser(seUser.getUserId());

        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<Map<String, Object>> dataList = dishonestyService.queryByPage(dishonestyParam);
        resultMap.put("dataList", dataList);
        resultMap.put("paramMap", dishonestyParam);
        return new ModelAndView(MY_LIST_PAGE, resultMap);
    }

    @RequestMapping(value = "/uncheck/dishonesty/view/{id}")
    public ModelAndView viewConfigInfo(@PathVariable String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        DishonestyVO dishonestyVO = dishonestyService.findById(id);
        resultMap.put("dishonesty", dishonestyVO);
        resultMap.put("discussType", 2);
        resultMap.put("discussTargetId", dishonestyVO.getDishonestyId());
        return new ModelAndView(VIEW_PAGE, resultMap);
    }
}
